package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.config.AppConfig;
import com.example.forum.model.Article;
import com.example.forum.model.ArticleReply;
import com.example.forum.model.User;
import com.example.forum.services.IArticleReplyService;
import com.example.forum.services.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: liuLi
 * Date: 2024-08-14
 * Time: 19:01
 */

@Api(tags = "回帖接口")
@Slf4j
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {

    @Resource
    private IArticleService articleService;

    @Resource
    private IArticleReplyService articleReplyService;



    /**
     * 回复帖子
     * @param articleId 帖子Id
     * @param content 回复内容
     * @return AppResult
     */
    @ApiOperation("回复帖子")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam(value = "帖子Id") @RequestParam(value = "articleId") @NonNull Long articleId,
                            @ApiParam(value = "回复内容") @RequestParam(value = "content") @NonNull String content) {
        // 获取⽤户信息
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        // 判断是否被禁⾔
        if (user.getState() == 1) {
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }

        // 获取帖⼦信息
        Article article = articleService.selectById(articleId);

        // 校验帖⼦信息
        if (article == null) {
            //帖子不存在
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        if (article.getState() == 1 || article.getDeleteState() == 1) {
            //帖子状态异常
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }

        // 构造数据
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId);
        articleReply.setPostUserId(user.getId());
        articleReply.setContent(content);
        articleReply.setReplyId(null);
        articleReply.setReplyUserId(null);
        // 调⽤业务层
        articleReplyService.create(articleReply);
        // 返回结果
        return AppResult.success();
    }


    /**
     * 回复帖子
     * @param articleId 帖子Id
     * @return AppResult
     */
    @ApiOperation("回复帖子")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(@ApiParam(value = "帖子Id") @RequestParam(value = "articleId") @NonNull Long articleId) {
        // 查询帖⼦是否存在
        Article article = articleService.selectById(articleId);
        if (article == null || article.getDeleteState() == 1) {

            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        // 查询结果
        List<ArticleReply> result = articleReplyService.selectByArticleId(articleId);
        // 返回结果
        return AppResult.success(result);
    }

}
